<?php
class Controller_Association extends Controller_Template
{
	public function __construct()
	{
		parent::__construct();
		$this->selfModel = new Model_MapperAssociation("association", parent::$db);
	}

	public function index()
	{
		$pager = new Pager($this->selfModel, Config::get_slot('associations_max_page', 10), Model_TableAssociation::count());
		$pager->setQuery('getAll');
		$pager->init();
		header('Content-Type: text/html; charset=utf-8');
		require('view/association/adminindex.php');
	}

        public function create()
	{
            $valid = false;
            $erreurs = array();//pour affichage des erreurs

            //affichage du formulaire
            $form = new Formulaire('association', '#', 'POST');

            $form->addAttribut('id', 'association_form');

            $nom = new ChampText('nomassociation', 'Nom', true);
            $nom->addValidateur(new ValidateurText(100));

            $description = new ChampTextArea('description', 'Description', false);
            $description->addValidateur(new ValidateurText(500));
            $description->addAttribut('cols', '50');
            $description->addAttribut('rows', '10');

            $telephone = new ChampText('telephone', 'Téléphone', true);
            $telephone->addValidateur(new ValidateurText(14));

            $fax = new ChampText('fax', 'Fax', false);
            $fax->addValidateur(new ValidateurText(14));
            
            $email = new ChampText('email', 'Email', true);
            $email->addValidateur(new ValidateurText(50));
            $email->addValidateur(new ValidateurMail());

            $responsable = new ChampText('responsable', 'Responsable', true);
            $responsable->addValidateur(new ValidateurText(50));

            $login = new ChampText('login', 'Login', true);
            $login->addValidateur(new ValidateurText(50));

            $password = new ChampPassword('password', 'Mot de passe', true);
            $password->addValidateur(new ValidateurText(50));

            $adresse1 = new ChampText('adresse1', 'Adresse 1', true);
            $adresse1->addValidateur(new ValidateurText(200));

            $adresse2 = new ChampText('adresse2', 'Adresse 2', false);
            $adresse2->addValidateur(new ValidateurText(200));

            $commune = new ChampList('commune', 'Commune', false, true);

            //récupération de la liste des communes
            $mappercommune = new Model_MapperCommune('commune', self::$db);
            $communes = $mappercommune->getAll();
            foreach($communes as $com)
            {
               $commune->addOption($com->getNom(), $com->getCode_postal() . ' ' .$com->getNom(), $com->getId_commune());
            }

            $submit = new ChampSubmit('submit', 'Valider');
            $submit->addAttribut('class', 'button');

            //groupe info
            $fieldinfo = new ChampFieldSet('info', 'Informations');
            $fieldinfo->addChamp($nom);
            $fieldinfo->addChamp($description);
            $fieldinfo->addChamp($telephone);
            $fieldinfo->addChamp($fax);
            $fieldinfo->addChamp($email);
            $fieldinfo->addChamp($responsable);

            //groupe adresse
            $fieldadresse = new ChampFieldSet('adresse', 'Adresse');
            $fieldadresse->addChamp($adresse1);
            $fieldadresse->addChamp($adresse2);
            $fieldadresse->addChamp($commune);

            //groupe authentification
            $fieldauth = new ChampFieldSet('auth', 'Authentification');
            $fieldauth->addChamp($login);
            $fieldauth->addChamp($password);

            //ajout des champs au formulaire
            $form->addChamp($fieldinfo);
            $form->addChamp($fieldadresse);
            $form->addChamp($fieldauth);
            $form->addChamp($submit);


            if(isset($_POST['formid']) && !empty($_POST['formid']) )
            {
                //formulaire soumis on recharge les valeurs et on valide le formulaire
                $form->bind();

                if($form->valider() )
                {
                    //ajout de l'association
                    $association = new Model_Association();
                    $association->setId_association(-1);
                    $association->setNom($nom->getValeur());
                    $association->setDescription($description->getValeur());
                    $association->setTelephone($telephone->getValeur());
                    $association->setFax($fax->getValeur());
                    $association->setEmail($email->getValeur());
                    $association->setResponsable($responsable->getValeur());
                    $association->setLogin($login->getValeur());
                    $association->setMd5Password($password->getValeur());

                    $adresse = new Model_Adresse();
                    $adresse->setId_adresse(-1);
                    $adresse->setAdresse1($adresse1->getValeur());
                    $adresse->setAdresse2($adresse2->getValeur());
                    $getcommune = $mappercommune->getOne($commune->getValeur());
                    $adresse->setCommune($getcommune);
                    $mapperadresse = new Model_MapperAdresse('adresse', self::$db);
                    $mapperadresse->save($adresse);

                    $association->setAdresse($adresse);

                    $this->selfModel->save($association);
                    $valid = true;
                }
                else
                {
                    //récupération des erreurs dans un tableau
                    //pour affichage dans la vue
                    foreach($form->getChamps() as $champ)
                    {
                        if($champ->isvalid == false)
                        {
                            $temp = array();
                            foreach($champ->getErreurs() as $erreur)
                            {
                                $temp[] = $erreur;
                            }
                            $erreurs[$champ->__toString()] = $temp;
                        }
                    }
                }
            }

            header('Content-Type: text/html; charset=utf-8');
            require('view/association/new-edit.php');
	}

	public function edit($id)
	{
            $association = $this->selfModel->getOne($id);
            $valid = false;
            $erreurs = array();//pour affichage des erreurs

            if(!is_null($association))
            {
                //affichage du formulaire
                $form = new Formulaire('association', '#', 'POST');

                $form->addAttribut('id', 'association_form');

                $nom = new ChampText('nomassociation', 'Nom', true);
                $nom->addValidateur(new ValidateurText(100));

                $description = new ChampTextArea('description', 'Description', false);
                $description->addValidateur(new ValidateurText(500));
                $description->addAttribut('cols', '50');
                $description->addAttribut('rows', '10');

                $telephone = new ChampText('telephone', 'Téléphone', true);
                $telephone->addValidateur(new ValidateurText(14));

                $fax = new ChampText('fax', 'Fax', false);
                $fax->addValidateur(new ValidateurText(14));

                $email = new ChampText('email', 'Email', true);
                $email->addValidateur(new ValidateurText(50));
                $email->addValidateur(new ValidateurMail());

                $responsable = new ChampText('responsable', 'Responsable', true);
                $responsable->addValidateur(new ValidateurText(50));

                $login = new ChampText('login', 'Login', true);
                $login->addValidateur(new ValidateurText(50));

                $password = new ChampPassword('password', 'Mot de passe', true);
                $password->addValidateur(new ValidateurText(50));

                $adresse1 = new ChampText('adresse1', 'Adresse 1', true);
                $adresse1->addValidateur(new ValidateurText(200));

                $adresse2 = new ChampText('adresse2', 'Adresse 2', false);
                $adresse2->addValidateur(new ValidateurText(200));

                $commune = new ChampList('commune', 'Commune', false, true);

                //récupération de la liste des communes
                $mappercommune = new Model_MapperCommune('commune', self::$db);
                $communes = $mappercommune->getAll();
                foreach($communes as $com)
                {
                   $commune->addOption($com->getNom(), $com->getCode_postal() . ' ' .$com->getNom(), $com->getId_commune());
                }

                $submit = new ChampSubmit('submit', 'Valider');
                $submit->addAttribut('class', 'button');

                //groupe info
                $fieldinfo = new ChampFieldSet('info', 'Informations');
                $fieldinfo->addChamp($nom);
                $fieldinfo->addChamp($description);
                $fieldinfo->addChamp($telephone);
                $fieldinfo->addChamp($fax);
                $fieldinfo->addChamp($email);
                $fieldinfo->addChamp($responsable);

                //groupe adresse
                $fieldadresse = new ChampFieldSet('adresse', 'Adresse');
                $fieldadresse->addChamp($adresse1);
                $fieldadresse->addChamp($adresse2);
                $fieldadresse->addChamp($commune);

                //groupe authentification
                $fieldauth = new ChampFieldSet('auth', 'Authentification');
                $fieldauth->addChamp($login);
                $fieldauth->addChamp($password);

                //modification on charge les valeurs de l'association
                $nom->bind($association->getNom());
                $description->bind($association->getDescription());
                $telephone->bind($association->getTelephone());
                $fax->bind($association->getFax());
                $email->bind($association->getEmail());
                $responsable->bind($association->getResponsable());
                $adresse1->bind($association->getAdresse()->getAdresse1());
                $adresse2->bind($association->getAdresse()->getAdresse2());
                $commune->bind($association->getAdresse()->getCommune()->getId_commune());
                $login->bind($association->getLogin());
                $password->bind($association->getPassword());


                //ajout des champs au formulaire
                $form->addChamp($fieldinfo);
                $form->addChamp($fieldadresse);
                $form->addChamp($fieldauth);
                $form->addChamp($submit);


                if(isset($_POST['formid']) && !empty($_POST['formid']) )
                {
                    //formulaire soumis on recharge les valeurs et on valide le formulaire
                    $form->bind();

                    if($form->valider() )
                    {
                        //ajout de l'association
                        $association->setNom($nom->getValeur());
                        $association->setDescription($description->getValeur());
                        $association->setTelephone($telephone->getValeur());
                        $association->setFax($fax->getValeur());
                        $association->setEmail($email->getValeur());
                        $association->setResponsable($responsable->getValeur());
                        $association->setLogin($login->getValeur());
                        $association->setMd5Password($password->getValeur());

                        $association->getAdresse()->setAdresse1($adresse1->getValeur());
                        $association->getAdresse()->setAdresse2($adresse2->getValeur());
                        $getcommune = $mappercommune->getOne($commune->getValeur());
                        $association->getAdresse()->setCommune($getcommune);
                        $mapperadresse = new Model_MapperAdresse('adresse', self::$db);
                        $mapperadresse->save($association->getAdresse());

                        //$association->setAdresse($adresse);

                        $this->selfModel->save($association);
                        $valid = true;
                    }
                    else
                    {
                        //récupération des erreurs dans un tableau
                        //pour affichage dans la vue
                        foreach($form->getChamps() as $champ)
                        {
                            if($champ->isvalid == false)
                            {
                                $temp = array();
                                foreach($champ->getErreurs() as $erreur)
                                {
                                    $temp[] = $erreur;
                                }
                                $erreurs[$champ->__toString()] = $temp;
                            }
                        }
                    }
                }

                header('Content-Type: text/html; charset=utf-8');
                require('view/association/new-edit.php');
            }
            else
            {
                    $message = "Association non trouvée";
                    header('Content-Type: text/html; charset=utf-8');
                    require('view/erreur/show.php');
            }
	}

        public function editinfo($id)
	{
            $association = $this->selfModel->getOne($id);
            $valid = false;
            $erreurs = array();//pour affichage des erreurs

            if(!is_null($association))
            {
                //affichage du formulaire
                $form = new Formulaire('association', '#', 'POST');

                $form->addAttribut('id', 'association_form');

                $nom = new ChampText('nomassociation', 'Nom', true);
                $nom->addValidateur(new ValidateurText(100));

                $description = new ChampTextArea('description', 'Description', false);
                $description->addValidateur(new ValidateurText(500));
                $description->addAttribut('cols', '50');
                $description->addAttribut('rows', '10');

                $telephone = new ChampText('telephone', 'Téléphone', true);
                $telephone->addValidateur(new ValidateurText(14));

                $fax = new ChampText('fax', 'Fax', false);
                $fax->addValidateur(new ValidateurText(14));

                $email = new ChampText('email', 'Email', true);
                $email->addValidateur(new ValidateurText(50));
                $email->addValidateur(new ValidateurMail());

                $responsable = new ChampText('responsable', 'Responsable', true);
                $responsable->addValidateur(new ValidateurText(50));

                $adresse1 = new ChampText('adresse1', 'Adresse 1', true);
                $adresse1->addValidateur(new ValidateurText(200));

                $adresse2 = new ChampText('adresse2', 'Adresse 2', false);
                $adresse2->addValidateur(new ValidateurText(200));

                $commune = new ChampList('commune', 'Commune', false, true);

                //récupération de la liste des communes
                $mappercommune = new Model_MapperCommune('commune', self::$db);
                $communes = $mappercommune->getAll();
                foreach($communes as $com)
                {
                   $commune->addOption($com->getNom(), $com->getCode_postal() . ' ' .$com->getNom(), $com->getId_commune());
                }

                $submit = new ChampSubmit('submit', 'Valider');
                $submit->addAttribut('class', 'button');

                //groupe info
                $fieldinfo = new ChampFieldSet('info', 'Informations');
                $fieldinfo->addChamp($nom);
                $fieldinfo->addChamp($description);
                $fieldinfo->addChamp($telephone);
                $fieldinfo->addChamp($fax);
                $fieldinfo->addChamp($email);
                $fieldinfo->addChamp($responsable);

                //groupe adresse
                $fieldadresse = new ChampFieldSet('adresse', 'Adresse');
                $fieldadresse->addChamp($adresse1);
                $fieldadresse->addChamp($adresse2);
                $fieldadresse->addChamp($commune);

                //modification on charge les valeurs de l'association
                $nom->bind($association->getNom());
                $description->bind($association->getDescription());
                $telephone->bind($association->getTelephone());
                $fax->bind($association->getFax());
                $email->bind($association->getEmail());
                $responsable->bind($association->getResponsable());
                $adresse1->bind($association->getAdresse()->getAdresse1());
                $adresse2->bind($association->getAdresse()->getAdresse2());
                $commune->bind($association->getAdresse()->getCommune()->getId_commune());

                //ajout des champs au formulaire
                $form->addChamp($fieldinfo);
                $form->addChamp($fieldadresse);
                $form->addChamp($submit);


                if(isset($_POST['formid']) && !empty($_POST['formid']) )
                {
                    //formulaire soumis on recharge les valeurs et on valide le formulaire
                    $form->bind();

                    if($form->valider() )
                    {
                        //ajout de l'association
                        $association->setNom($nom->getValeur());
                        $association->setDescription($description->getValeur());
                        $association->setTelephone($telephone->getValeur());
                        $association->setFax($fax->getValeur());
                        $association->setEmail($email->getValeur());
                        $association->setResponsable($responsable->getValeur());

                        $association->getAdresse()->setAdresse1($adresse1->getValeur());
                        $association->getAdresse()->setAdresse2($adresse2->getValeur());
                        $getcommune = $mappercommune->getOne($commune->getValeur());
                        $association->getAdresse()->setCommune($getcommune);
                        $mapperadresse = new Model_MapperAdresse('adresse', self::$db);
                        $mapperadresse->save($association->getAdresse());

                        //$association->setAdresse($adresse);

                        $this->selfModel->save($association);
                        $valid = true;
                    }
                    else
                    {
                        //récupération des erreurs dans un tableau
                        //pour affichage dans la vue
                        foreach($form->getChamps() as $champ)
                        {
                            if($champ->isvalid == false)
                            {
                                $temp = array();
                                foreach($champ->getErreurs() as $erreur)
                                {
                                    $temp[] = $erreur;
                                }
                                $erreurs[$champ->__toString()] = $temp;
                            }
                        }
                    }
                }

                header('Content-Type: text/html; charset=utf-8');
                require('view/association/editinfo.php');
            }
            else
            {
                    $message = "Association non trouvée";
                    header('Content-Type: text/html; charset=utf-8');
                    require('view/erreur/show.php');
            }
	}


        public function delete($id)
	{
		$association = $this->selfModel->getOne($id);

		if(!is_null($association))
		{
                    $this->selfModel->delete($association);
                    header('Content-Type: text/html; charset=utf-8');
                    require('view/association/delete.php');
		}
		else
		{
			$message = "Association non trouvée";
			header('Content-Type: text/html; charset=utf-8');
			require('view/erreur/show.php');
		}
	}
}

?>
